﻿namespace Blaze.Framework.Loaders
{
    using UnityEngine;

    /// <summary>
    /// 默认资源清单仓库实现。
    /// </summary>
    public class AssetBundleManifestRepository : IAssetBundleManifestRepository
    {
        /// <summary>
        /// 获取或设置Manifest的文件名。
        /// </summary>
        public static string ManifestFileName = "assetbundles";

        #region IAssetBundleManifestRepository Members

        /// <summary>
        /// 获取指定资源所在的<see cref="AssetBundleManifest"/>。
        /// </summary>
        public AssetBundleManifest Get(string uri)
        {
            if (mCachedManifest == null)
            {
                var manifestLoader = AssetLoaderPool.Get<AssetBundleManifestLoader>(ManifestFileName)
                    .AsSync()
                    .FluentStart();
                mCachedManifest = manifestLoader.Manifest;
                AssetLoaderPool.Put(manifestLoader);
            }
            return mCachedManifest;
        }

        /// <summary>
        /// 重置仓库缓存，当清单文件被重写时调用。
        /// </summary>
        public void Invalidate()
        {
            mCachedManifest = null;
        }

        #endregion

        private AssetBundleManifest mCachedManifest;
    }
}